import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter/material.dart';
import 'package:kepu/page/animal_detail_page/animal_detail_page.dart';
import 'package:kepu/page/plant_detail_page/plant_detail_page.dart';
import 'package:kepu/page/recommend_page/provider/recommend_page_provider.dart';
import 'package:kepu/provider/time_provider.dart';
import 'package:kepu/util/constant/constants.dart';
import 'package:kepu/widgets/error_widget.dart';
import 'package:kepu/widgets/loading_widget.dart';

import '../../widgets/image_with_title.dart';

class RecommendPage extends ConsumerWidget {
  const RecommendPage({super.key});


  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var greetingTime = ref.watch(timeProvider);
    var recommendEntity = ref.watch(recommendPageProviderProvider);
    return recommendEntity.when(
      error: (e, st) => errorWidget(),
      loading: () => loadWidget(context),
      data:
          (itemList) => Scrollbar(
            child: Padding(
              padding: const EdgeInsets.fromLTRB(10, 0, 10, 10),
              child: CustomScrollView(
                slivers: [
                  //问候文字区
                  SliverToBoxAdapter(
                    child: Container(
                      margin: EdgeInsets.only(top: 15, bottom: 30),
                      child: Text(
                        "Hello~ $greetingTime!",
                        style: TextStyle(
                          fontSize: 25,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                  //图片网格区
                  SliverGrid.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                      mainAxisExtent: 280,
                    ),
                    itemCount: itemList.data.length,
                    itemBuilder: (context, index) {
                      return GestureDetector(
                        onTap: () {
                          //跳转到详情页
                          if (itemList.data[index].plant == false) {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder:
                                    (_) => AnimalDetailPage(
                                      itemList.data[index].id,
                                    ),
                              ),
                            );
                          } else {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder:
                                    (_) => PlantDetailPage(
                                      itemList.data[index].id,
                                    ),
                              ),
                            );
                          }
                        },
                        child: ImageWithTitle(
                          title: itemList.data[index].name,
                          location: itemList.data[index].region,
                          imageUrl: imageUrlPrefix + itemList.data[index].image,
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),
    );
  }
}
